<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="${path}/static/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="${path}/static/css/font-awesome.min.css">
    <link rel="stylesheet" href="${path}/static/css/main.css">
    <style>
        .tree li {
            list-style-type: none;
            cursor:pointer;
        }
        table tbody tr:nth-child(odd){background:#F4F4F4;}
        table tbody td:nth-child(even){color:#C00;}
    </style>
</head>

<body>

<%-- 顶部导航 --%>
<jsp:include page="${path}/static/nav.jsp"></jsp:include>

<div class="container-fluid">
    <div class="row">
        
        <%--  左侧导航栏  --%>
        <jsp:include page="${path}/static/left.jsp"></jsp:include>
        
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="likeValue" class="form-control has-success" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button type="button" onclick="liek()" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询</button>
                    </form>
                    <button type="button" id="btndeleteAll" class="btn btn-danger" style="float:right;margin-left:10px;"><i class=" glyphicon glyphicon-remove"></i> 删除</button>
                    <!-- 按钮触发模态框 -->
                    <%--<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">新增</button>--%>
                    <button type="button" class="btn btn-primary" style="float:right;" data-toggle="modal" data-target="#myModal"><i class="glyphicon glyphicon-plus"></i> 新增</button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <form method="post" id="checkboxForms"  >
                        <table class="table  table-bordered">
                            <thead>
                                <tr >
                                    <th width="30">#</th>
                                    <th width="30"><input id="checkboxFlag" type="checkbox" ></th>
                                    <th>编号</th>
                                    <th>账号</th>
                                    <th>名称</th>
                                    <th width="100">操作</th>
                                </tr>
                            </thead>

                                <tbody id="bodyData">

                                </tbody>

                            <tfoot>
                                <tr>
                                    <td colspan="6" align="center">
                                        <ul class="pagination" id="pageData">

                                        </ul>
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<!-- 增加用户模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">×
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    请填写用户信息
                </h4>
            </div>
            <div class="modal-body">
                <form role="form" id="addUserForm">
                    <div class="form-group">
                        <label for="nickname">登陆账号</label>
                        <input type="text" class="form-control" name="username" id="nickname" placeholder="请输入登陆账号">
                        <p class="help-block label label-warning">注意：用户名长度小于等于5</p>
                    </div>
                    <div class="form-group">
                        <label for="username">用户名称</label>
                        <input type="text" class="form-control" name="nickname" id="username" placeholder="请输入姓名">
                        <p class="help-block label label-warning">注意：新用户初始密码为123456</p>
                    </div>

                    <button type="button" onclick="addUser()" class="btn btn-success"><i class="glyphicon glyphicon-plus"></i> 新增</button>
                    <button type="button" class="btn btn-danger"><i class="glyphicon glyphicon-refresh"></i> 重置</button>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">关闭
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script src="${path}/static/jquery/jquery-2.1.1.min.js"></script>
<script src="${path}/static/bootstrap/js/bootstrap.min.js"></script>
<script src="${path}/static/script/docs.min.js"></script>
<script src="${path}/static/layer/layer.js"></script>

<script type="text/javascript">
    $(function () {
        $(".list-group-item").click(function(){
            if ( $(this).find("ul") ) {
                $(this).toggleClass("tree-closed");
                if ( $(this).hasClass("tree-closed") ) {
                    $("ul", this).hide("fast");
                } else {
                    $("ul", this).show("fast");
                }
            }
        });
        showList(1);
    });


    function liek(){
        showList(1);
    }

    function showList(pageNum) {
        var data = $("#likeValue").val();
        $.ajax({
            url:"${path}/user/list",
            method:"post",
            data:{"pageNum":pageNum,"like":data},
            datatype:"json",
            error:function (err) {
                alert("失败")
            },
            success:function (obj) {
                var bodyHtml = "";
                $.each(obj.userList,function (index,user) {
                    bodyHtml += "<tr>";
                    bodyHtml += "<td>"+(index+1)+"</td>";
                    bodyHtml += "<td><input type='checkbox' name='userid' value='"+user.userid+"' class='username' ></td>";
                    bodyHtml += "<td>"+user.userid+"</td>";
                    bodyHtml += "<td>"+user.username+"</td>";
                    bodyHtml += "<td>"+user.nickname+"</td>";
                    bodyHtml += "<td>";
                    bodyHtml += "<button type='button' class='btn btn-success btn-xs' onclick='assignRoleView("+user.userid+")' ><i class=' glyphicon glyphicon-check'></i></button>";
                    bodyHtml += "<button type='button' class='btn btn-primary btn-xs' onclick='updateView("+user.userid+")'><i class=' glyphicon glyphicon-check'></i></button>";
                    bodyHtml += "<button type='button' class='btn btn-danger btn-xs' onclick='deleteData("+user.userid+")' ><i class=' glyphicon glyphicon-remove'></i></button>";
                    bodyHtml += "</td>";
                    bodyHtml += "</tr>";
                })
                $("#bodyData").html(bodyHtml);

                var pageHtml = "";
                pageHtml += "<li class='"+(obj.pageInfo.pageNum==1?'disabled':'')+"'><a onclick='showList("+1+")' >首页</a></li>";
                pageHtml += "<li class='"+(obj.pageInfo.pageNum==1?'disabled':'')+"'><a onclick='showList("+obj.pageInfo.perPage+")' >上一页</a></li>";
                for(var i = 1; i <= obj.pageInfo.pages ; i++){
                    pageHtml +="<li class='"+(obj.pageInfo.pageNum==i?'active':'')+"'><a onclick='showList("+i+")'>"+i+" <span class='sr-only'>(current)</span></a></li>";
                }
                pageHtml += "<li class='"+(obj.pageInfo.pageNum==obj.pageInfo.pages?'disabled':'')+"'><a onclick='showList("+(obj.pageInfo.pageNum==obj.pageInfo.pages?obj.pageInfo.pages:obj.pageInfo.nextPage)+")' >下一页</a></li>";
                pageHtml += "<li class='"+(obj.pageInfo.pageNum==obj.pageInfo.pages?'disabled':'')+"'><a onclick='showList("+obj.pageInfo.pages+")' >末页</a></li>";
                $("#pageData").html(pageHtml);

            }
        });
    }

    function addUser() {
        $.ajax({
            url:"${path}/user/add",
            method:"post",
            data:$("#addUserForm").serialize(),
            datatype:"json",
            error:function (err) {
                alert("失败")
            },
            success:function(obj) {
                if (obj.result){
                    layer.msg("添加成功", {time:1000, icon:6, shift:3}, function(){
                        showList(1);
                    });
                }else{
                    layer.msg("添加失败", {time:1000, icon:5, shift:6});
                }
            }
        });
    }

    function deleteData(Id){
        alert(Id);
        $.ajax({
            url:"${path}/user/delete",
            method:"post",
            data:{"userid":Id},
            datatype:"json",
            error:function (err) {
                alert("失败")
            },
            success:function (obj) {
                if (obj.result){
                    layer.msg("删除成功", {time:1000, icon:6, shift:3}, function(){
                        showList(1);
                    });
                }else{
                    layer.msg("删除失败", {time:1000, icon:5, shift:6});
                }
            }
        });
    }

    function assignRoleView(userid) {
        window.location.href = "${path}/userrole/assignrole?userid="+userid;
    }

    function updateView(userid){
        window.location.href = "${path}/user/updateView?userid="+userid;
    }

    $("#checkboxFlag").click(function () {
        if ($(this).prop("checked")!=true){
            $(".username").prop("checked", false);
        }else{
            $(".username").prop("checked", true);
        }
    });

    $("#btndeleteAll").click(function () {
        $.ajax({
            url:"${path}/user/deleteAll",
            method:"post",
            data:$("#checkboxForms").serialize(),
            error:function (err) {
                alert("失败");
            },
            success:function (obj) {
                if(obj=="true"){
                    layer.msg("删除成功", {time:1000, icon:6, shift:3}, function(){
                        showList(1);
                    });
                }else{
                    layer.msg("删除失败", {time:1000, icon:5, shift:6});
                }
            }
        })
    });


</script>
</body>
</html>
